package org.zpmis.api.business;

import cn.hutool.core.lang.UUID;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zpmis.common.base.AdminLoginUser;
import org.zpmis.common.base.BaseWebService;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.ResultCode;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.domain.company.enums.CompanyEnum;
import org.zpmis.domain.company.model.Company;
import org.zpmis.domain.company.qo.*;
import org.zpmis.domain.company.service.CompanyService;
import org.zpmis.domain.company.vo.CompanyListVo;
import org.zpmis.domain.company.vo.CompanyTrackVo;
import org.zpmis.domain.company.vo.GetCompanyDetailVo;
import org.zpmis.domain.contract.enums.ContractEnum;
import org.zpmis.domain.contract.model.Contract;
import org.zpmis.domain.contract.service.ContractService;
import org.zpmis.domain.contract.vo.ContractVo;
import org.zpmis.domain.customer.enums.CustomerPlanEnum;
import org.zpmis.domain.customer.enums.ReturnVisitEnum;
import org.zpmis.domain.customer.model.CustomerPlan;
import org.zpmis.domain.customer.model.Intention;
import org.zpmis.domain.customer.model.ReturnVisit;
import org.zpmis.domain.customer.service.CustomerPlanService;
import org.zpmis.domain.customer.service.IntentionService;
import org.zpmis.domain.customer.service.ReturnVisitService;
import org.zpmis.domain.customer.vo.IntentionTrackVo;
import org.zpmis.domain.user.model.User;
import org.zpmis.domain.user.service.UserService;
import org.zpmis.domain.user.service.WxApiService;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Created with CodeGenerator
 * Description: 企业信息 业务处理类
 * @author  Yang
 * Date: 2021-6-2
 * Time: 14:46:52
 */
@Service
@Slf4j
public class CompanyWebService extends BaseWebService {

     @Reference
     private CompanyService companyService;

     @Reference
     private UserService userService;

     @Reference
     private WxApiService wxApiService;

     @Reference
     private IntentionService intentionService;

     @Reference
     private ReturnVisitService returnVisitService;

     @Reference
     private CustomerPlanService customerPlanService;

     @Reference
     private ContractService contractService;

     /**
      * 企业注册
      * @param registerCompanyQo
      * @throws Exception
      */
     @Transactional
     public void registerCompany(RegisterCompanyQo registerCompanyQo) throws Exception{
          if(StringUtil.isEmpty(registerCompanyQo.getCompanyCode())){
               throw new BusinessException(ResultCode.COMPANY_CODE_NOT_EMPTY);
          }
          //根据id主键判断是注册还是修改
          Long id = registerCompanyQo.getId();
          if(id != null){
               Company company = companyService.findById(id);
               if(company == null){
                    throw new BusinessException(ResultCode.DATA_NOT_FOUND);
               }
               company.setName(registerCompanyQo.getName());
               company.setContactPerson(registerCompanyQo.getContactPerson());
               company.setContactMobile(registerCompanyQo.getContactMobile());
               company.setRegionCode(registerCompanyQo.getRegionCode());
               company.setCreditCode(registerCompanyQo.getCreditCode());
               company.setLegalPerson(registerCompanyQo.getLegalPerson());
               company.setOrgCode(registerCompanyQo.getOrgCode());
               company.setRegisterAddress(registerCompanyQo.getRegisterAddress());
               company.setEmail(registerCompanyQo.getEmail());
               company.setBelongIndustry(registerCompanyQo.getBelongIndustry());
               company.setCompanyType(registerCompanyQo.getCompanyType());
               company.setRegisterCapital(registerCompanyQo.getRegisterCapital());
               company.setBusinessScope(registerCompanyQo.getBusinessScope());
               company.setBankName(registerCompanyQo.getBankName());
               company.setBankAccountName(registerCompanyQo.getBankAccountName());
               company.setBankAccount(registerCompanyQo.getBankAccount());
               company.setUserType(registerCompanyQo.getUserType());
               company.setIsDeleted(CompanyEnum.NOT_DELETE.code());
               company.setStatus(CompanyEnum.ENABLE_STATUS.code());
               company.setCustomerSource(registerCompanyQo.getCustomerSource());
               company.setRemark(registerCompanyQo.getRemark());
               company.setAuditStatus(CompanyEnum.PROCESS_STATUS.code());
               company.setAttachment(registerCompanyQo.getAttachment());
               company.setUpdateTime(Instant.now().toEpochMilli());
               //company.setCompanyCode(registerCompanyQo.getCompanyCode());
               company.setAgentId(registerCompanyQo.getAgentId());
               /**
                * 修改企业信息
                */
               companyService.update(company);
          }else{
               //id为空则是注册企业
               Condition condition = new Condition(Company.class);
               Example.Criteria criteria = condition.createCriteria();
               criteria.andEqualTo("isDeleted",CompanyEnum.NOT_DELETE.code());
               criteria.andEqualTo("companyCode",registerCompanyQo.getCompanyCode());
               List<Company> companyList = companyService.findByCondition(condition);
               if(!companyList.isEmpty()){
                    throw new BusinessException(ResultCode.COMPANY_DATA_ALREADY_EXISTS);
               }
               //通过微信code 获取openid
               String wxOpenId = wxApiService.getWxOpenIdByCode(registerCompanyQo.getWxCode());
               if(StringUtil.isEmpty(wxOpenId)){
                    throw new BusinessException(ResultCode.USER_NOT_EXIST);
               }
               Map<String,Object> param = new HashMap<>();
               param.put("wxOpenid",wxOpenId);
               param.put("isDeleted",0);
               List<User> userList = userService.findByConditionMap(param);
               if(!userList.isEmpty()){
                    //用户已存在 防止重复注册
                    throw  new BusinessException(ResultCode.USER_ACCOUNT_ALREADY_EXIST);
               }
               //如果填写用户名,则判断用户名是否重复
               if(StringUtil.isNotEmpty(registerCompanyQo.getUserName())){
                    Map<String,Object> paramUserName = new HashMap<>();
                    paramUserName.put("userName",registerCompanyQo.getUserName());
                    paramUserName.put("isDeleted",0);
                    List<User> users = userService.findByConditionMap(paramUserName);
                    if(!users.isEmpty()){
                         //用户名已存在 防止重复用户名
                         throw  new BusinessException(ResultCode.USER_NAME_ALREADY_EXIST);
                    }
               }
               Company company = Company.builder()
                       .name(registerCompanyQo.getName())
                       .contactPerson(registerCompanyQo.getContactPerson())
                       .contactMobile(registerCompanyQo.getContactMobile())
                       .regionCode(registerCompanyQo.getRegionCode())
                       .creditCode(registerCompanyQo.getCreditCode())
                       .legalPerson(registerCompanyQo.getLegalPerson())
                       .orgCode(registerCompanyQo.getOrgCode())
                       .registerAddress(registerCompanyQo.getRegisterAddress())
                       .email(registerCompanyQo.getEmail())
                       .belongIndustry(registerCompanyQo.getBelongIndustry())
                       .companyType(registerCompanyQo.getCompanyType())
                       .registerCapital(registerCompanyQo.getRegisterCapital())
                       .businessScope(registerCompanyQo.getBusinessScope())
                       .bankName(registerCompanyQo.getBankName())
                       .bankAccountName(registerCompanyQo.getBankAccountName())
                       .bankAccount(registerCompanyQo.getBankAccount())
                       .userType(registerCompanyQo.getUserType())
                       .isDeleted(CompanyEnum.NOT_DELETE.code())
                       .status(CompanyEnum.ENABLE_STATUS.code())
                       .customerSource(registerCompanyQo.getCustomerSource())
                       .remark(registerCompanyQo.getRemark())
                       .auditStatus(CompanyEnum.PROCESS_STATUS.code())
                       .attachment(registerCompanyQo.getAttachment())
                       .createTime(Instant.now().toEpochMilli())
                       .updateTime(Instant.now().toEpochMilli())
                       .companyCode(registerCompanyQo.getCompanyCode())
                       .agentId(registerCompanyQo.getAgentId())
                       .build();
               /**
                * 保存企业信息
                */
               companyService.save(company);


               /**
                * 保存企业用户登录信息
                */
               userService.save(
                       User.builder()
                               .name(company.getName())
                               .companyId(company.getId())
                               .userName(registerCompanyQo.getUserName())
                               .wxOpenid(wxOpenId)
                               .password(StringUtil.isEmpty(registerCompanyQo.getPassword()) ? "" : new BCryptPasswordEncoder().encode(registerCompanyQo.getPassword()))
                               .createTime(Instant.now().toEpochMilli())
                               .updateTime(Instant.now().toEpochMilli())
                               .createUser(company.getId())
                               .build()
               );
          }
     }

     /**
      * 注册企业审核
      * @param auditCompanyQo
      */
     public void auditCompany(AuditCompanyQo auditCompanyQo) throws BusinessException,Exception{
          //获取当前登录用户
          AdminLoginUser adminLoginUser = super.getAdminLoginUser();
          if(adminLoginUser == null){
               throw new BusinessException(ResultCode.USER_NOT_LOGIN);
          }
          Integer auditStatus = auditCompanyQo.getAuditStatus();
          Long id = auditCompanyQo.getId();
          Company company = companyService.findById(id);
          if(company == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          companyService.update(
                  Company.builder()
                  .id(id)
                  .auditStatus(auditStatus)
                  .auditPerson(adminLoginUser.getUserName())
                  .auditTime(Instant.now().toEpochMilli())
                  .auditUid(adminLoginUser.getId())
                  .updateTime(Instant.now().toEpochMilli())
                  .build()
          );
     }

     /**
      * 企业列表查询
      * @param companyListQo
      * @return
      */
     public PageInfo companyList(CompanyListQo companyListQo) throws Exception{
          String companyName = companyListQo.getCompanyName();
          Integer status = companyListQo.getStatus();
          Integer auditStatus = companyListQo.getAuditStatus();
          String contactMobile = companyListQo.getContactMobile();
          Long startTime = companyListQo.getStartTime();
          Long endTime = companyListQo.getEndTime();
          String legalPerson = companyListQo.getLegalPerson();
          String orgCode = companyListQo.getOrgCode();
          Integer userType = companyListQo.getUserType();
          String auditPerson = companyListQo.getAuditPerson();
          String contactPerson = companyListQo.getContactPerson();
          Long agentId = companyListQo.getAgentId();
          Condition condition = new Condition(Company.class);
          Example.Criteria criteria = condition.createCriteria();
          criteria.andEqualTo("isDeleted",CompanyEnum.NOT_DELETE.code());
          if(agentId != null){
               criteria.andEqualTo("agentId",agentId);
          }
          if(StringUtil.isNotEmpty(companyName)){
               criteria.andLike("name","%"+companyName+"%");
          }
          if(StringUtil.isNotEmpty(legalPerson)){
               criteria.andLike("legalPerson","%"+legalPerson+"%");
          }
          if(StringUtil.isNotEmpty(orgCode)){
               criteria.andLike("orgCode","%"+orgCode+"%");
          }
          if(userType != null){
               criteria.andEqualTo("userType",userType);
          }
          if(StringUtil.isNotEmpty(auditPerson)){
               criteria.andLike("auditPerson","%"+auditPerson+"%");
          }
          if(StringUtil.isNotEmpty(contactPerson)){
               criteria.andLike("contactPerson","%"+contactPerson+"%");
          }
          if(StringUtil.isNotEmpty(contactMobile)){
               criteria.andLike("contactMobile","%"+contactMobile+"%");
          }
          if(status != null){
               criteria.andEqualTo("status",status);
          }
          if(auditStatus != null){
               criteria.andEqualTo("auditStatus",auditStatus);
          }
          if(startTime != null && endTime != null){
               criteria.andGreaterThanOrEqualTo("createTime",startTime);
               criteria.andLessThanOrEqualTo("createTime",endTime);
          }
          if(startTime != null && endTime == null){
               criteria.andGreaterThanOrEqualTo("createTime",startTime);
               criteria.andLessThanOrEqualTo("createTime",Instant.now().toEpochMilli());
          }
          if(startTime == null && endTime != null){
               criteria.andLessThanOrEqualTo("createTime",endTime);
          }

          condition.orderBy("createTime").desc();
          PageHelper.startPage(companyListQo.getPageNum(),companyListQo.getPageSize());
          List<Company> companyList = companyService.findByCondition(condition);
          PageInfo pageInfo = new PageInfo<>(companyList);
          //设置自定义返回对象
          List<CompanyListVo> resultList = companyList.stream().map(CompanyListVo::new).collect(Collectors.toList());
          pageInfo.setList(resultList);
          return pageInfo;
     }

     /**
      * 企业列表查询 不分页
      * @param
      * @return
      */
     public List<CompanyListVo> companyAllList(CompanyAllListQo companyAllListQo) throws Exception{

          Condition condition = new Condition(Company.class);
          Example.Criteria criteria = condition.createCriteria();
          criteria.andEqualTo("isDeleted",CompanyEnum.NOT_DELETE.code());
          criteria.andEqualTo("auditStatus",CompanyEnum.APPROVED_STATUS.code());
          if(StringUtil.isNotEmpty(companyAllListQo.getCompanyName())){
               criteria.andLike("name","%"+companyAllListQo.getCompanyName()+"%");
          }
          if(companyAllListQo.getUserType() != null){
               criteria.andEqualTo("userType",companyAllListQo.getUserType());
          }
          condition.orderBy("createTime").desc();
          List<Company> companyList = companyService.findByCondition(condition);
          //设置自定义返回对象
          List<CompanyListVo> resultList = companyList.stream().map(CompanyListVo::new).collect(Collectors.toList());
          return resultList;
     }

     /**
      * 所有通过的代理商下的企业列表查询,下拉使用(包括自己) 不分页
      * @param
      * @return
      */
     public List<CompanyListVo> agentCompanyAllList(AgentCompanyAllListQo agentCompanyAllListQo) throws Exception{

          Condition condition = new Condition(Company.class);
          Example.Criteria criteria = condition.createCriteria();
          criteria.andEqualTo("isDeleted",CompanyEnum.NOT_DELETE.code());
          criteria.andEqualTo("auditStatus",CompanyEnum.APPROVED_STATUS.code());
          if(StringUtil.isNotEmpty(agentCompanyAllListQo.getCompanyName())){
               criteria.andLike("name","%"+agentCompanyAllListQo.getCompanyName()+"%");
          }
          if(agentCompanyAllListQo.getCompanyId() != null){
               criteria.andEqualTo("agentId",agentCompanyAllListQo.getCompanyId());
          }
          condition.orderBy("createTime").desc();
          List<Company> companyList = new ArrayList<>();
          //添加自己
          Company company = companyService.findByModel(
             new HashMap<String, Object>() {{
               put("isDeleted", CompanyEnum.NOT_DELETE.code());
               put("auditStatus", CompanyEnum.APPROVED_STATUS.code());
               put("id", agentCompanyAllListQo.getCompanyId());
          }});
          companyList.add(company);
          companyList.addAll(companyService.findByCondition(condition));
          //设置自定义返回对象 并去重 防止自己本身就是自己的代理的问题
          List<CompanyListVo> resultList = companyList.stream().map(CompanyListVo::new).filter(distinctByKey(CompanyListVo::getId)).collect(Collectors.toList());
          return resultList;
     }
     /**
      * 企业状态变更
      * @param changeStatusQo
      */
     public void changeStatus(ChangeStatusQo changeStatusQo){
          Long id = changeStatusQo.getId();
          Company company = companyService.findById(id);
          if(company == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          Company updateCompany = Company.builder().id(id).build();
          Integer status = company.getStatus();
          if(status == CompanyEnum.DISABLE_STATUS.code()){
               updateCompany.setStatus(CompanyEnum.ENABLE_STATUS.code());
               log.info("[CompanyWebService]---changeStatus--当前企业已经启用-->company:{}",company);
          }
          if(status == CompanyEnum.ENABLE_STATUS.code()){
               updateCompany.setStatus(CompanyEnum.DISABLE_STATUS.code());
               log.info("[CompanyWebService]---changeStatus--当前企业已经被禁用-->company:{}",company);
          }
          updateCompany.setUpdateTime(Instant.now().toEpochMilli());
          companyService.update(updateCompany);
     }

     /**
      *  企业轨迹查询, 只查询对应意向的回访和计划信息,可以根据意向用户的id查询
      * @param companyTrackQo
      * @return
      * @throws BusinessException
      * @throws Exception
      */
     public CompanyTrackVo companyTrack(CompanyTrackQo companyTrackQo) throws BusinessException,Exception{
          //根据企业code查询企业信息
          Map<String, Object> param = new HashMap<>();
          param.put("isDeleted",CompanyEnum.NOT_DELETE.code());
          param.put("companyCode",companyTrackQo.getCompanyCode());
          Company company = companyService.findByModel(param);
          if(company == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          CompanyTrackVo companyTrackVo = new CompanyTrackVo(company);
          //查询意向信息
          Intention intention = intentionService.findByModel(param);
          if(intention != null){
               companyTrackVo.setIntentionContactMobile(intention.getContactMobile());
               companyTrackVo.setIntentionContactPerson(intention.getContactPerson());
               companyTrackVo.setIntentionConpanyName(intention.getName());
               companyTrackVo.setIntentionCreateTime(intention.getCreateTime() == null ? "" : LocalDateTime.ofInstant(Instant.ofEpochMilli(intention.getCreateTime()), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
          }

          //查询企业的意向回访集合
          Condition returnVisitCondition = new Condition(ReturnVisit.class);
          returnVisitCondition.createCriteria()
                  .andEqualTo("isDeleted", ReturnVisitEnum.NOT_DELETE.code())
                  .andEqualTo("visitObject", ReturnVisitEnum.VISIT_OBJECT_INTENTION.code())
                  .andEqualTo("companyCode",companyTrackQo.getCompanyCode());
          List<ReturnVisit> returnVisitList = returnVisitService.findByCondition(returnVisitCondition);
          List<IntentionTrackVo> visitTrackVoList = returnVisitList.stream().map(IntentionTrackVo::new).collect(Collectors.toList());
          //查询企业的意向计划集合
          Condition customerPlanCondition = new Condition(CustomerPlan.class);
          customerPlanCondition.createCriteria()
                  .andEqualTo("isDeleted", CustomerPlanEnum.NOT_DELETE.code())
                  .andEqualTo("visitObject", CustomerPlanEnum.VISIT_OBJECT_INTENTION.code())
                  .andEqualTo("companyCode",companyTrackQo.getCompanyCode());
          List<CustomerPlan> customerPlanList = customerPlanService.findByCondition(customerPlanCondition);
          List<IntentionTrackVo> planTrackVoList = customerPlanList.stream().map(IntentionTrackVo::new).collect(Collectors.toList());
          List<IntentionTrackVo> resultTrackVoList = new ArrayList<>();
          resultTrackVoList.addAll(visitTrackVoList);
          resultTrackVoList.addAll(planTrackVoList);
          //按照时间排序
          List<IntentionTrackVo> sortedList = resultTrackVoList.stream().sorted(Comparator.comparing(IntentionTrackVo::getVisitTime, Comparator.nullsFirst(Long::compareTo))).collect(Collectors.toList());
          companyTrackVo.setVisitPlanList(sortedList);
          //查询合同集合
         /* Condition condition = new Condition(Contract.class);
          Example.Criteria criteria = condition.createCriteria();
          criteria.andEqualTo("isDeleted",ContractEnum.NOT_DELETE.code());
          criteria.andEqualTo("companyId",company.getId());*/
          //condition.orderBy("createTime").asc();
          Map<String, Object> contractParam = new HashMap<>();
          contractParam.put("isDeleted",CompanyEnum.NOT_DELETE.code());
          contractParam.put("companyId",company.getId());
          List<Contract> contractList = contractService.findByConditionMap(contractParam);
          List<ContractVo> contractVoList = contractList.stream().sorted(Comparator.comparing(Contract::getCreateTime, Comparator.nullsFirst(Long::compareTo))).map(ContractVo::new).collect(Collectors.toList());
          companyTrackVo.setContractVoList(contractVoList);
          return companyTrackVo;
     }

     /**
      * 根据企业id获取企业详情信息
      * @param getCompanyDetailQo
      * @return
      * @throws BusinessException
      * @throws Exception
      */
     public GetCompanyDetailVo getCompanyDetail(GetCompanyDetailQo getCompanyDetailQo) throws BusinessException,Exception{
          Long id = getCompanyDetailQo.getId();
          Company company = companyService.findById(id);
          if(company == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          return GetCompanyDetailVo.toCompanyDetailVo(company);
     }

     /**
      * 后台新增企业
      * @param registerCompanyQo
      */
     @Transactional
     public void addCompany(RegisterCompanyQo registerCompanyQo) {

          //如果填写用户名,则判断用户名是否重复
          if(StringUtil.isNotEmpty(registerCompanyQo.getUserName())){
               Map<String,Object> paramUserName = new HashMap<>();
               paramUserName.put("userName",registerCompanyQo.getUserName());
               paramUserName.put("isDeleted",0);
               List<User> users = userService.findByConditionMap(paramUserName);
               if(!users.isEmpty()){
                    //用户名已存在 防止重复用户名
                    throw  new BusinessException(ResultCode.USER_NAME_ALREADY_EXIST);
               }
          }
          Company company = Company.builder()
                  .name(registerCompanyQo.getName())
                  .contactPerson(registerCompanyQo.getContactPerson())
                  .contactMobile(registerCompanyQo.getContactMobile())
                  .regionCode(registerCompanyQo.getRegionCode())
                  .creditCode(registerCompanyQo.getCreditCode())
                  .legalPerson(registerCompanyQo.getLegalPerson())
                  .orgCode(registerCompanyQo.getOrgCode())
                  .registerAddress(registerCompanyQo.getRegisterAddress())
                  .email(registerCompanyQo.getEmail())
                  .belongIndustry(registerCompanyQo.getBelongIndustry())
                  .companyType(registerCompanyQo.getCompanyType())
                  .registerCapital(registerCompanyQo.getRegisterCapital())
                  .businessScope(registerCompanyQo.getBusinessScope())
                  .bankName(registerCompanyQo.getBankName())
                  .bankAccountName(registerCompanyQo.getBankAccountName())
                  .bankAccount(registerCompanyQo.getBankAccount())
                  .userType(registerCompanyQo.getUserType())
                  .isDeleted(CompanyEnum.NOT_DELETE.code())
                  .status(CompanyEnum.ENABLE_STATUS.code())
                  .customerSource(registerCompanyQo.getCustomerSource())
                  .remark(registerCompanyQo.getRemark())
                  .auditStatus(CompanyEnum.PROCESS_STATUS.code())
                  .attachment(registerCompanyQo.getAttachment())
                  .createTime(Instant.now().toEpochMilli())
                  .updateTime(Instant.now().toEpochMilli())
                  .companyCode(UUID.randomUUID().toString().replaceAll("-",""))
                  .agentId(registerCompanyQo.getAgentId())
                  .build();
          /**
           * 保存企业信息
           */
          companyService.save(company);

          /**
           * 保存企业用户登录信息
           */
          userService.save(
                  User.builder()
                          .name(company.getName())
                          .companyId(company.getId())
                          .userName(registerCompanyQo.getUserName())
                          //.wxOpenid(wxOpenId)
                          .password(StringUtil.isEmpty(registerCompanyQo.getPassword()) ? "" : new BCryptPasswordEncoder().encode(registerCompanyQo.getPassword()))
                          .createTime(Instant.now().toEpochMilli())
                          .updateTime(Instant.now().toEpochMilli())
                          .createUser(company.getId())
                          .build()
          );
     }





     /**
      * 代理修改企业信息
      * @param registerCompanyQo
      */
     public void agentUpdateCompany(RegisterCompanyQo registerCompanyQo) throws Exception,BusinessException{
          Company company = companyService.findById(registerCompanyQo.getId());
          if(company == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          company.setName(registerCompanyQo.getName());
          company.setContactPerson(registerCompanyQo.getContactPerson());
          company.setContactMobile(registerCompanyQo.getContactMobile());
          company.setRegionCode(registerCompanyQo.getRegionCode());
          company.setCreditCode(registerCompanyQo.getCreditCode());
          company.setLegalPerson(registerCompanyQo.getLegalPerson());
          company.setOrgCode(registerCompanyQo.getOrgCode());
          company.setRegisterAddress(registerCompanyQo.getRegisterAddress());
          company.setEmail(registerCompanyQo.getEmail());
          company.setBelongIndustry(registerCompanyQo.getBelongIndustry());
          company.setCompanyType(registerCompanyQo.getCompanyType());
          company.setRegisterCapital(registerCompanyQo.getRegisterCapital());
          company.setBusinessScope(registerCompanyQo.getBusinessScope());
          company.setBankName(registerCompanyQo.getBankName());
          company.setBankAccountName(registerCompanyQo.getBankAccountName());
          company.setBankAccount(registerCompanyQo.getBankAccount());
          company.setUserType(registerCompanyQo.getUserType());
          company.setIsDeleted(CompanyEnum.NOT_DELETE.code());
          company.setStatus(CompanyEnum.ENABLE_STATUS.code());
          company.setCustomerSource(registerCompanyQo.getCustomerSource());
          company.setRemark(registerCompanyQo.getRemark());
          company.setAuditStatus(CompanyEnum.PROCESS_STATUS.code());
          company.setAttachment(registerCompanyQo.getAttachment());
          company.setUpdateTime(Instant.now().toEpochMilli());
          //company.setCompanyCode(registerCompanyQo.getCompanyCode());
          company.setAgentId(registerCompanyQo.getAgentId());
          /**
           * 修改企业信息
           */
          companyService.update(company);
     }
}
